package com.example.basewarehouse.utils;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import androidx.core.app.ActivityCompat;

import com.example.basewarehouse.app.Constants;

import static android.content.Context.TELEPHONY_SERVICE;


public class PhoneInfoUtils {

    /**
     * 1、获取手机品牌
     * @param context
     * @return
     */
    public static String getBrand(Context context) {

        String brand = null;

        try {

            brand = Build.BRAND;

        } catch (Exception e) {

            e.printStackTrace();

        }

        return brand;

    }

    /**
     * 2、获取手机型号
     * @param context
     * @return
     */
    public static String getModel(Context context) {

        String model = null;

        try {

            model = Build.MODEL;

        } catch (Exception e) {

            e.printStackTrace();

        }

        return model;

    }

    /**
     * 获取手机分辨率宽度
     * @param context
     * @return
     */
    public static int getWidth(Context context) {

        int width = 0;

        try {

            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

            DisplayMetrics dm = new DisplayMetrics();

            windowManager.getDefaultDisplay().getMetrics(dm);

            int screenWidthDip = dm.widthPixels;

            width = screenWidthDip;

        } catch (Exception e) {

            e.printStackTrace();

        }

        return width;

    }

    /**
     * 获取手机android系统版本
     * @param context
     * @return
     */
    public static String getAndroidVersion(Context context) {

        String version = null;

        try {

            version = Build.VERSION.RELEASE;

        } catch (Exception e) {

            e.printStackTrace();

        }

        return version + "";

    }

    /**
     * 6、获取手机imei
     */


    protected static String getImei(Context context) {

        String mImei = "NULL";

        try {

            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return mImei;
            }
            mImei = ((TelephonyManager) context

                    .getSystemService(TELEPHONY_SERVICE)).getDeviceId();

        } catch (Exception e) {

            mImei = "NULL";

        }

        return mImei;

    }

    /**
     * 获取当前手机系统版本号
     *
     * @return 系统版本号
     */
    public static String getSystemVersion() {
        return Build.VERSION.RELEASE;
    }

    /**
     * 获取手机型号
     *
     * @return 手机型号
     */
    public static String getSystemModel() {
        return Build.MODEL;
    }

    /**
     * 获取手机IMEI(需要“android.permission.READ_PHONE_STATE”权限)
     *
     * @return 手机IMEI
     */
    public static String getIMEI(Context ctx) {
        TelephonyManager tm = (TelephonyManager) ctx.getSystemService(TELEPHONY_SERVICE);
        if (tm != null) {
            if (ActivityCompat.checkSelfPermission(ctx, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return "null";
            }
            return tm.getDeviceId();
        }
        return null;
    }

    public static String packageName(Context context) {
        PackageManager manager = context.getPackageManager();
        String name = null;
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            name = info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return name;
    }

    public static int packageCode(Context context) {
        PackageManager manager = context.getPackageManager();
        int code = 0;
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            code = info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return code;
    }

    public static String getMacAddress() {

        String macAddress =null;
        WifiManager wifiManager =
                (WifiManager) Constants.context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = (null== wifiManager ?null: wifiManager.getConnectionInfo());

        if(!wifiManager.isWifiEnabled())
        {
            //必须先打开，才能获取到MAC地址
            wifiManager.setWifiEnabled(true);
            wifiManager.setWifiEnabled(false);
        }
        if(null!= info) {
            macAddress = info.getMacAddress();
        }
        return macAddress;
    }
//     public static String getDeviceId(){
//         return PushServiceFactory.getCloudPushService().getDeviceId();
//     }

     public static String getDevicedId(){
         TelephonyManager telephonyManager = (TelephonyManager)Constants.context.getSystemService(Context.TELEPHONY_SERVICE);
         @SuppressLint({"HardwareIds", "MissingPermission"}) String id = telephonyManager.getDeviceId();
         return id;
     }


}
